package com.szholly.pro.watergas_server.ps.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.ps.service.CompanyInfoService;
import com.szholly.pro.watergas_server.ps.service.RemoteService;
import com.szholly.data.general.RowBase;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/CompanyInfo/*")
public class CompanyInfoController extends BaseController { 
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	RemoteService remoteService;
	
	@Autowired
	CompanyInfoService companyInfoService;
	
	/**
	 * 获取工业污染源企业列表信息，左边导航
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/companyList")
	public void queryCompanyList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		List<RowBase> rows = companyInfoService.queryCompanyList();
		String result = DataUtil.listToJSONObject(rows).toString();
		outString(result);
	}
	
	/**
	 * 获取企业列表，包含其下包含的站点数
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/companyStationCountList")
	public void companyStationCountList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		List<RowBase> rows = companyInfoService.companyStationCountInfoList(resq,res);
		String result = DataUtil.listToJSONObject(rows).toString();
		outString(result);
	}
	
	//查询左边列表污染企业详细信息
	@RequestMapping(value = "/companyDetail")
	public void queryCompanyDetail(HttpServletRequest req, HttpServletResponse res) throws Exception {
		List<RowBase> companyDetailList = companyInfoService.queryCompanyDetail (req);
		//查询企业污染源类型
		String companyCode = req.getParameter("COMPANY_CODE");
		List<RowBase> pollType = companyInfoService.queryCompanyPollType(companyCode);
		List<RowBase> pollTypeChecked = companyInfoService.queryCompanyPollTypeChecked(companyCode);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("companyDetail",companyDetailList);
		map.put("POLL_TYPE",pollType);		
		map.put("POLL_TYPE_CHECKED",pollTypeChecked);
		JSONObject json = JSONObject.fromObject(map);
		outString(json.toString());
	}
	
	/**
	 * 新增企业信息 BC_ENTERPRISE_ATTR,company_polltype
	 * 熊伟
	 * 2016年06月14日
	 * @param res
	 */
	@RequestMapping (value = "/createCompany")
	public void createCompany(HttpServletResponse res){
		try {
			companyInfoService.createCompanyInfo();
			String jsonStr = "{\"data\":\"success\"}";
			outString(jsonStr);
			} catch (Exception e) {
				e.printStackTrace();
				outErrorString( e.getMessage());
			}
	}
	
	/**
	 * 更新企业信息
	 * 2015年12月21日
	 * @param res
	 * @throws Exception 
	 */
	@RequestMapping (value = "/updateCompany")
	public void updateCompany(HttpServletResponse res){
		try {
			companyInfoService.updateCompanyInfo();
			String jsonStr = "{\"data\":\"success\"}";
			outString(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			outErrorString( e.getMessage());
		}
	}

}
